package cn.com.open.itembank.logic.questiontype;

import cn.com.open.itembank.dto.QuestionImportModel;
import cn.com.open.itembank.entity.QuestionType;

/**
 * Created by lucky on 2017/12/19.
 */
public class CompositeItem extends BaseSubItem {

    /**
     * :stantiates a new Composite item.
     *
     * @param model the model
     */
    public CompositeItem(QuestionImportModel model) {
        super(model);
    }

    //region 校验分数

    /**
     * 校验分数.
     *
     * @return the boolean
     */
    @Override
    public boolean checkScoreFormat() {
        boolean result = true;

        Double scoreSum = 0d;
        for (int i = 0; i < importModel.getSubItems().size(); i++) {
            QuestionImportModel item = importModel.getSubItems().get(i);
            Double score = 0d;
            try {
                score = Double.parseDouble(item.getScore());
            } catch (Exception e) {
                importModel.setErrorInfo(String.format("第 %d 道子题分值格式不正确；", i + 1));
                result = false;
            }

            scoreSum += score;
        }

        if (scoreSum == 0) {
            importModel.setErrorInfo("分值不能为零；");
            result = false;
        }

        if (scoreSum < importModel.getSubItems().size()) //分数要大于等于子试题数
        {
            importModel.setErrorInfo("子试题分数不匹配；");
            result = false;
        }

        return result;
    }
    //endregion

    //region 校验子题型

    /**
     * 校验子题型.
     *
     * @return the boolean
     */
    @Override
    protected boolean checkSubQuestionType() {
        boolean result = true;

        for (int i = 0; i < importModel.getSubItems().size(); i++) {
            QuestionImportModel item = importModel.getSubItems().get(i);

            for (QuestionType qt : questionTypes) {
//                if (!qt.getQuestionTypeName().equalsIgnoreCase(item.getQuestionTypeName())) {
//                    importModel.setErrorInfo(String.format("第 %d 道子题没有该子题型:%s；", i + 1, item.getQuestionType()));
//                    result = false;
//                    break;
//                }
                if(qt.getQuestionTypeName().equalsIgnoreCase(item.getQuestionTypeName())){
                    return true;
                }
            }
        }

        return result;
    }
    //endregion
}
